package FlexTrade;

import java.rmi.RemoteException;
import java.util.Hashtable;

public class AuthenticateImpl implements IAuthenticate {
	/**
	 * This is authentication service. Before participating in auction
	 * both customer/provider need to be authenticated
	 */
	private static final long serialVersionUID = 1L;

	//pre-defined authentication for customers
	//private Hashtable customerAuthenticate;
	//private Hashtable providerAuthenticate;
	private Hashtable<String, String> customerAuthenticate;
	
	//pre-defined authentication for providers
	private Hashtable<String, String> providerAuthenticate;
	
	public AuthenticateImpl() {
		// TODO Auto-generated constructor stub
		customerAuthenticate = new Hashtable<String, String>();
		providerAuthenticate = new Hashtable<String, String>();
		initialize();
	}
	
	@Override
	public boolean authenticateCustomer(String username, String password)
			throws RemoteException {
		// TODO Auto-generated method stub
		//check to see if username/password is valid or not
		String t_customer = customerAuthenticate.get(username);
		if(t_customer == null){
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean authenticateProvider(String username, String password)
			throws RemoteException {
		// TODO Auto-generated method stub
		//check to see if username/password is valid or not
		String t_provider = providerAuthenticate.get(username);
		if(t_provider == null){
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean addCustomerAccount(String username, String password)
			throws RemoteException {
		// TODO Auto-generated method stub
		String t_customer = customerAuthenticate.get(username);
		if(t_customer == null){
			customerAuthenticate.put(username, password);
			return true;
		} else {
			System.out.println("Add new user error. Username already exists.");
			return false;
		}
		
	}

	@Override
	public boolean addProviderAccount(String username, String password)
			throws RemoteException {
		// TODO Auto-generated method stub
		String t_provider = providerAuthenticate.get(username);
		if(t_provider == null){
			providerAuthenticate.put(username, password);
			return true;
		} else {
			System.out.println("Add new provider error. Username already exists");
			return false;
		}
	}
	
	private void initialize(){
		String temp_customer;
		String temp_provider;
		for(int i = 1; i <= 4; i++){
			temp_customer = "customer" + Integer.toString(i);
			temp_provider = "provider" + Integer.toString(i);
			customerAuthenticate.put(temp_customer, "");
			providerAuthenticate.put(temp_provider, "");
		}
	}
}
